package sample.SendRouterOrder;

/**
 * 作者：w on 2021-08-15.
 * 邮箱：y141111@126.com
 * 版本：v1.0
 */
public class SendRouter {
    private static byte[] order = new byte[]{
            (byte)0xaa, (byte)0xfa, // 包头0~1
            (byte)0x48, (byte)0x4e, (byte)0x53, (byte)0x30, (byte)0x30, (byte)0x35, // 识别码(2~7)
            0,  // 第几个点(8)
            0,0,0,0,0,0,0,0,0,0,0,0,0,    // 纬度(9~21)
            0,0,0,0,0,0,0,0,0,0,0,0,0,0,  // 经度(22~35)
            0,  // 转弯方向(36)
            (byte)0x55};
    public static byte[] recv_order =  new byte[]{
            (byte)0xaa, (byte)0xfa, // 包头0~1
            (byte)0x48, (byte)0x4e, (byte)0x53, (byte)0x30, (byte)0x30, (byte)0x35, // 识别码(2~7)
            0,  // 第几个点(8)
            0,0,0,0,0,0,0,0,0,0,0,0,0,    // 纬度(9~21)
            0,0,0,0,0,0,0,0,0,0,0,0,0,0,  // 经度(22~35)
            0,  // 转弯方向(36)
            (byte)0x55};

    /**
     * 设置识别码 字符串
     * @param str   eg: HNS005
     */
    public static boolean setEpNameString(String str){
        byte[] bytes = str.getBytes();
        if (bytes.length == 6){
            recv_order[2] = order[2] = bytes[0];
            recv_order[3] = order[3] = bytes[1];
            recv_order[4] = order[4] = bytes[2];
            recv_order[5] = order[5] = bytes[3];
            recv_order[6] = order[6] = bytes[4];
            recv_order[7] = order[7] = bytes[5];
            if (onChangeListener != null){
                onChangeListener.onChange();
            }
            return true;
        }else {
            return false;
        }
    }
    /**
     * 设置识别码 bytes
     * @param bytes   eg: new byte[]{(byte)0x48, (byte)0x4e, (byte)0x53, (byte)0x30, (byte)0x30, (byte)0x35}
     */
    public static boolean setEpNameBytes(byte[] bytes){
        if (bytes.length == 6){
            recv_order[2] = order[2] = bytes[0];
            recv_order[3] = order[3] = bytes[1];
            recv_order[4] = order[4] = bytes[2];
            recv_order[5] = order[5] = bytes[3];
            recv_order[6] = order[6] = bytes[4];
            recv_order[7] = order[7] = bytes[5];
            if (onChangeListener != null){
                onChangeListener.onChange();
            }
            return true;
        }else {
            return false;
        }
    }

    /**
     * 获取识别码字符串
     * @return eg. HNS005
     */
    public static String getEpName(){
        return new String(new byte[]{order[2], order[3], order[4], order[5], order[6], order[7]});
    }

    /**
     * 设置第几个点
     * @param num 第几个点
     */
    public static void setNum(byte num){
        order[8] = num;
        if (onChangeListener != null){
            onChangeListener.onChange();
        }
    }
    /**
     * 设置纬度 长度 13
     * @param lat String 类型 纬度 长度 13
     */
    public static void setLat(String lat){
        if (lat.length() == 13){
            byte[] bytes = lat.getBytes();
            System.arraycopy(bytes, 0, order, 9, bytes.length);
            if (onChangeListener != null){
                onChangeListener.onChange();
            }
        }
    }
    /**
     * 设置经度 长度 14
     * @param Long String 类型 经度 长度 14
     */
    public static void setLong(String Long){
        if (Long.length() == 14){
            byte[] bytes = Long.getBytes();
            System.arraycopy(bytes, 0, order, 22, bytes.length);
            if (onChangeListener != null){
                onChangeListener.onChange();
            }
        }
    }
    /**
     * 设置左拐右拐180
     * 00代表左转90
     * 01代表右转90
     * 02代表转弯180
     * @param Dir byte 类型 经度 长度 13
     */
    public static void setDir(byte Dir){
        order[36] = Dir;
        if (onChangeListener != null){
            onChangeListener.onChange();
        }
    }


    /**
     * 获取当前控制指令的所有状态数组
     * @return byte数组
     */
    public static byte[] getOrder(){
        return order;
    }

    /**
     * 获取 控制命令变量的16禁止字符串
     * @return eg: AAAA0A00000000000A
     */
    public static String getOrderHexString(){
        StringBuilder str = new StringBuilder();
        for (byte i: order){
            String tmp = Integer.toHexString(i & 0xff);
            str.append(tmp.length() > 1 ? tmp : "0" + tmp);
        }
        return str.toString().toUpperCase();    // str.toUpperCase() 转换成大写
    }
    public interface OnChangeListener {
        void onChange();    // 值改变
    }
    private static OnChangeListener onChangeListener;
    public static void setOnChangeListener(OnChangeListener onChange){
        onChangeListener = onChange;
    }
}
